import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { DataEvent } from "../events/DataEvent";
import { BuyAlert } from "../view/pet/BuyAlert";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { Language } from "../../../extend/language/Language";
import { TodayNotTipUtil } from "../../common/TodayNotTipUtil";
import { TodayNoTipsConst } from "../../component/gconst/TodayNoTipsConst";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { SBargainShopPlayerItemInfo } from "../../../Message/Public/SBargainShopPlayerItemInfo";
import { BargainShopConfig } from "../resource/BargainShopConfig";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { DiscountShopBintBtn } from "../view/dicountShop/DiscountShopBintBtn";
import { IView } from "../../mvc/interfaces/IView";
import { DiscountShopModules } from "../../../modules/DiscountShopModules";
type int = number;
//class DiscountShopController
    
    export  class DiscountShopController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.OnDiscountShopOpen, this.onOpenDiscountModule);
            Dispatcher.addEventListener(EventName.BuyDiscountShopItem, this.toBuyItem);
            Dispatcher.addEventListener(EventName.ToRefreshDiscountShop, this.toRefresh);
            Dispatcher.addEventListener(EventName.DiscountShopActiveTimeUp, this.onActiveTimeUP);
            NetDispatcher.addCmdListener(ServerCommand.DiscountShopInfo, this.onActiveOpen);
            NetDispatcher.addCmdListener(ServerCommand.DiscountShopOpenInfo, this.onInfoReady);
            NetDispatcher.addCmdListener(ServerCommand.DiscountShopRefresh, this.onShopInfoChange);
            NetDispatcher.addCmdListener(ServerCommand.DiscountShopBuyItem, this.onShopInfoChange);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveListChange);
            return;
        }

        /* internal  */onActiveListChange(arg1: Object=null): void
        {
            var loc1=cache.active.getActiveByCode(EActiveType._EActiveTypeBargainShop);
            if (loc1 == null) 
            {
                this.closeHandler();
            }
            return;
        }

        /* internal  */onActiveTimeUP(arg1: DataEvent): void
        {
            this.closeHandler();
            return;
        }

        /* internal  */updateMoney(arg1: Object=null): void
        {
            if (this.module) 
            {
                this.module.updateMoney();
            }
            return;
        }

        /* internal  */toRefresh(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1=this.cache.discountShop.getRefreshCost();
            var loc2=BuyAlert.checkMoney(EPrictUnit._EPriceUnitGold, loc1);
            if (loc2) 
            {
                loc3 = Language.getStringByParam(70674, loc1);
                TodayNotTipUtil.toDayNotTip(TodayNoTipsConst.DiscountShopRefresh, this.callBackHandler, loc3, null, Alert.OK | Alert.CANCEL, null);
            }
            return;
        }

        /* internal  */callBackHandler(): void
        {
            GameProxy.discountShop.refreshShop();
            return;
        }

        /* internal  */toBuyItem(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.discountShop.buyShopItem(loc1.id, loc1.amount);
            return;
        }

        /* internal  */onInfoReady(arg1: SBargainShopPlayerItemInfo): void
        {
            if (this.module && this.module.isHide) 
            {
                this.module.show();
            }
            this.onShopInfoChange(arg1);
            return;
        }

        /* internal  */onShopInfoChange(arg1: SBargainShopPlayerItemInfo): void
        {
            var loc1=null;
            var loc2=null;
            this.cache.discountShop.info = arg1;
            if (this.module && !this.module.isHide) 
            {
                loc1 = BargainShopConfig.instance.getCurListDataBySInfo(arg1);
                loc2 = {"info": arg1, "list": loc1};
                this.module.updateInfo(loc2);
            }
            return;
        }

        /* internal  */onOpenDiscountModule(arg1: DataEvent): void
        {
            GameProxy.discountShop.viewShopInfo();
            return;
        }

        /* internal  */onActiveOpen(arg1: SActiveToClient): void
        {
            this.cache.discountShop.activeInfo = arg1;
            if (this.cache.active.isActiveInEffectByInfo(arg1)) 
            {
                if (!this._hintBtn) 
                {
                    this._hintBtn = new DiscountShopBintBtn();
                }
                this._hintBtn.show();
            }
            else 
            {
                this.closeHandler();
            }
            return;
        }

        /* internal  */closeHandler(): void
        {
            if (this._hintBtn && !this._hintBtn.isHide) 
            {
                this._hintBtn.hide();
            }
            if (this.module && !this.module.isHide) 
            {
                this.closeInterv = flash.utils.setTimeout(this.onDelayClose, 10000);
            }
            return;
        }

        /* internal  */onDelayClose(): void
        {
            if (this.module && !this.module.isHide) 
            {
                this.module.hide();
            }
            flash.utils.clearTimeout(this.closeInterv);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._view) 
            {
                this._view = new DiscountShopModules();
            }
            return this._view;
        }

        /* internal  */get module(): DiscountShopModules
        {
            return this.view as DiscountShopModules;
        }

        private /* var */_hintBtn: DiscountShopBintBtn;

        private /* var */closeInterv: int;
    }
